<!-- pages/login.vue -->
<template>
  <section class="index flex">
  <div class="w-full bg-gray-100 flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
      <h2 class="text-2xl font-bold mb-6 text-center">国有资本后端管理系统</h2>
      <Notification :message="errorMessage" v-if="errorMessage" />
      <form @submit.prevent="handleLogin">
        <div class="">
          <input type="text" id="username" placeholder="账号" v-model="username" class="Input mt-1 block w-full  rounded-md  " required>
        </div>
        <div class="line"></div>
        <div class="">
          <input type="password" id="password" placeholder="密码" v-model="password" class="Input mt-1 block w-full  rounded-md  " required>
        </div>
        <div class="line"></div>
        <div class="text-right  mt-1 ">
          <a href="#" class="text-sm text-indigo-500 hover:text-indigo-600">忘记密码？</a>
        </div>

        <div class="flex items-center justify-between mt-6 ">
          <button  type="submit" class="bg-indigo-500 text-white py-2 px-4 w-full rounded-md hover:bg-indigo-600 focus:outline-none focus:ring-2 focus:ring-indigo-500">登录</button>
        </div>

      </form>
      <div class="mt-6 text-center">
        <span class="text-gray-600">还没有账号？</span>
        <!-- <NuxtLink to="/register" class="text-indigo-500 hover:text-indigo-600">立即注册</NuxtLink> -->
      </div>
    </div>
  </div>
</section>
</template>

<script setup lang="ts">
definePageMeta({
  layout: 'empty',
});
import { login  } from '~/api/users';
const username = ref('')
const password = ref('')
const errorMessage = ref('')
import { useAuthStore } from '@/stores/auth';
const authStore = useAuthStore();
authStore.logout();
const handleLogin = async () => {
  // 处理登录逻辑
  const para = {
    username: username.value,
    password: password.value,
  }
  const res = await login(para)
  if(res) {
    authStore.setToken(res.data.token);
    authStore.setUser(JSON.stringify(res.data.user));
    authStore.setId(res.data.user._id);
    navigateTo('/')
  }
}
</script>


<style lang="stylus" scoped>
.index {
  width 100%
}
</style>